package com.ms.rc_lab.ui.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.ms.rc_lab.CommandCenter;
import com.ms.rc_lab.RockerView;
import com.ms.rc_lab.TcpClient;
import com.ms.rc_lab.databinding.FragmentHomeBinding;

import java.util.Timer;
import java.util.TimerTask;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    String operateDes = CommandCenter.CAR;

    int pwm3Angle = 0;
    @SuppressLint("ClickableViewAccessibility")
    void initUI(){
        binding.rockerLeft.setOnShakeListener(RockerView.DirectionMode.DIRECTION_8, new RockerView.OnShakeListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void direction(RockerView.Direction direction, int size) {
                if(size > 99){
                    size = 99;
                }
                Log.d("ROCKER", String.format("direction, size= %s %d", CommandCenter.getDirection(direction), size));
                TcpClient.getInstance().sendMessage(CommandCenter.commandCat(CommandCenter.CAR, CommandCenter.getDirection(direction), size));
            }

            @Override
            public void onFinish() {
                TcpClient.getInstance().sendMessage(CommandCenter.commandCat(CommandCenter.CAR, CommandCenter.CMD_CENTER, 0));
            }
        });
        binding.vseekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int curProgress = (int)((progress - 50)/50.0f * 90);
                String cmd = CommandCenter.commandCat(CommandCenter.PWM, String.valueOf(0), curProgress);
                TcpClient.getInstance().sendMessage(cmd);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        binding.hseekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int curProgress = (int)((progress - 50)/50.0f * 90);
                String cmd = CommandCenter.commandCat(CommandCenter.PWM, String.valueOf(1), curProgress);
                TcpClient.getInstance().sendMessage(cmd);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        binding.btnRotateLeft.setOnTouchListener(new View.OnTouchListener() {
            Timer timer1 = null;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        timer1 = new Timer();
                        TimerTask timerTask1 = new TimerTask() {
                            @Override
                            public void run() {
                                synchronized (this){
                                    // 按下事件
                                    if(operateDes.equals(CommandCenter.CAR)){
                                        TcpClient.getInstance().sendMessage(CommandCenter.commandCat(operateDes, CommandCenter.ROTATE_CLOCKWISE, 99));
                                    }else {
                                        pwm3Angle -= 5;
                                        if (pwm3Angle <= -90) {
                                            pwm3Angle = -90;
                                        }
                                        TcpClient.getInstance().sendMessage(CommandCenter.commandCat(operateDes, String.valueOf(CommandCenter.PWM_C3), pwm3Angle));
                                    }
                                }
                            }
                        };
                        timer1.scheduleAtFixedRate(timerTask1, 0, 500);

                        break;
                    case MotionEvent.ACTION_UP:
                        timer1.cancel();
                        // 弹起事件
                        if(operateDes.equals(CommandCenter.CAR)){
                            TcpClient.getInstance().sendMessage(CommandCenter.commandCat(operateDes, CommandCenter.CMD_CENTER, 0));
                        }
                        break;
                }
                return true; // 返回true表示已消费事件，不再传递
            }
        });

        binding.btnRotateRight.setOnTouchListener(new View.OnTouchListener() {
            Timer timer2 = null;
            TimerTask timerTask2 = null;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        timer2 = new Timer();
                        timerTask2 = new TimerTask() {
                            @Override
                            public void run() {
                                synchronized (this){

                                    // 按下事件
                                    if(operateDes.equals(CommandCenter.CAR)){
                                        String message = CommandCenter.commandCat(operateDes, CommandCenter.ROTATE_COUNTERCLOCKWISE, 99);
                                        TcpClient.getInstance().sendMessage(message);
                                    }else {
                                        pwm3Angle += 5;
                                        if(pwm3Angle >= 90 ){
                                            pwm3Angle = 90;
                                        }
                                        String message = CommandCenter.commandCat(operateDes, String.valueOf(CommandCenter.PWM_C3), pwm3Angle);
                                        TcpClient.getInstance().sendMessage(message);
                                    }
                                }
                            }
                        };
                        timer2.scheduleAtFixedRate(timerTask2, 0, 500);

                        break;
                    case MotionEvent.ACTION_UP:
                        timer2.cancel();
                        // 弹起事件
                        if(operateDes.equals(CommandCenter.CAR)){
                            String message = CommandCenter.commandCat(operateDes, CommandCenter.CMD_CENTER, 0);
                            TcpClient.getInstance().sendMessage(message);
                        }
                        break;
                }
                return true; // 返回true表示已消费事件，不再传递
            }
        });


        binding.switchDes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    operateDes = CommandCenter.PWM;
                }else {
                    operateDes = CommandCenter.CAR;
                }
            }
        });
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        initUI();
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}